package com.tdma.g12.provider;


import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;

import com.tdam.g12.soporte.Constantes;
import com.tdam.g12.soporte.Contacto;
import com.tdma.g12.sql.ContactoSMSWebHelper;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

public class SMSWebProvider extends ContentProvider
{
	//Definicion del CONTENT_URI
	private static final String uri = "content://com.tdma.g12.mjewebprovider";
	public static final Uri CONTENT_URI = Uri.parse(uri);
	
	private ContactoSMSWebHelper _dataBaseHelper;
	
	Contacto contacto = null;
	
	public void setContacto (Contacto contacto)
	{
		this.contacto = contacto;
	}

	@Override
	public int delete (Uri uri, String selection, String[] selectionArgs)
	{
		// TODO Auto-generated method stub
		if (contacto != null)
		{
			_dataBaseHelper.DelContactoSMSWeb(contacto) ;
			return 1;	
		}
		return 0;
	}

	@Override
	public String getType(Uri uri)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values)
	{
		// TODO Auto-generated method stub
		if (values.size() > 0)
		{
			Object object = null;
			try{
				object = new ObjectInputStream(new ByteArrayInputStream(values.getAsByteArray(Constantes.CONTACTO_WEB))).readObject();
			}
			catch(java.io.IOException ioe){
				ioe.getMessage();
			}catch(java.lang.ClassNotFoundException cnfe){
			cnfe.getMessage();
			}
			contacto = (Contacto)object;  
			_dataBaseHelper.AddContactoSMSWeb(contacto);	
		}
		return null;
	}

	@Override
	public boolean onCreate()
	{
		// TODO Auto-generated method stub
		_dataBaseHelper = new ContactoSMSWebHelper(getContext());
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] idContactoWeb, String sortOrder)
	{
		// TODO Auto-generated method stub
		Cursor cursor = _dataBaseHelper.getContactoSMSWeb(idContactoWeb);
		return cursor ;//_dataBaseHelper.getContactoSMSWeb(idContactoWeb);
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs)
	{
		// TODO Auto-generated method stub
		_dataBaseHelper.UpdContactoSMSWeb((Contacto)values.get(Constantes.CONTACTO_WEB));
		return 0;
	}

}
